package com.gjs.common.manager.notification.sms.Impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.gjs.common.manager.notification.NotificationFailException;
import com.gjs.common.manager.notification.sms.ISms;
import com.gjs.common.manager.notification.sms.SmsMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class AliyunSms implements ISms {

    @Autowired
    private Client client;

    @Override
    public void send(SmsMessage smsMessage) throws NotificationFailException {
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(smsMessage.getAliyunSignName())
                .setTemplateCode(smsMessage.getTemplateCode())
                .setTemplateParam(smsMessage.getContent())
                .setPhoneNumbers(smsMessage.getTarget());
        try {
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            SendSmsResponseBody sendSmsResponseBody = sendSmsResponse.getBody();
            if(!sendSmsResponseBody.getCode().equals("OK")){
                throw new NotificationFailException(sendSmsResponseBody.getMessage());
            }
        }catch (Exception e){
            throw new NotificationFailException(e.getMessage());
        }
    }
}
